﻿package {
	import com.janisRadins.asDraw.DrawUtils;
	import com.janisRadins.asDraw.Path;
	import com.janisRadins.asDraw.PathCollection;
	import flash.display.GraphicsPath;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	public class T3 extends Sprite {
		private var xmlData:XML = null;
		private var loader:URLLoader = null;
		public function T3() {
			loader = new URLLoader(new URLRequest("SVGPath2.svg?T=" + (new Date().getTime())));
			loader.addEventListener(Event.COMPLETE, onXMLLoaded);
		}
		private function onXMLLoaded(e:Event):void {
			xmlData = XML(loader.data);
			var svgNs:Namespace = new Namespace("http://www.w3.org/2000/svg");
			
			var pathVect:Vector.<Path> = new Vector.<Path>();
			for each(var pathXML:XML in xmlData..svgNs::path) {
				pathVect.push(DrawUtils.svgToPath(pathXML));
			}
			
			var pathCol:PathCollection = new PathCollection(pathVect);
			
			var commands:GraphicsPath = pathCol.getCurveSegment();
			var drawSpr:Sprite = new Sprite();
			drawSpr.graphics.lineStyle(2, 0xFF0000, 0.7);
			drawSpr.graphics.beginFill(0x123456, 0.5);
			drawSpr.graphics.drawPath(commands.commands, commands.data, commands.winding);
			drawSpr.graphics.endFill();
			addChild(drawSpr);
			
			var pointSpr:Sprite = new Sprite();
			pointSpr.graphics.beginFill(0x123456);
			pointSpr.graphics.drawCircle(0, 0, 3);
			addChild(pointSpr);
			var linePos:Number = 0;
			
			var subCurveSpr:Sprite = new Sprite();
			addChild(subCurveSpr);
			var curvePos:Number = 0;
			addEventListener(Event.ENTER_FRAME, function(e:Event):void {
				linePos += 10;
				linePos %= pathCol.length;
				var p:Point = pathCol.getPointOnCurve(linePos);
				pointSpr.x = p.x;
				pointSpr.y = p.y;
				
				curvePos += 5;
				curvePos %= pathCol.length;
				var cmd:GraphicsPath = pathCol.getCurveSegment(curvePos, (curvePos + 150) % pathCol.length);
				subCurveSpr.graphics.clear();
				subCurveSpr.graphics.lineStyle(2, 0x654321);
				subCurveSpr.graphics.drawPath(cmd.commands, cmd.data, cmd.winding)
			});
			
		}
	}
}